'use strict';
const path = require('path');
const vSource = require('vinyl-source-stream');
const vBuffer = require('vinyl-buffer');
const browserify = require('browserify');
const watchify = require('watchify');
const babelify = require('babelify');
const vueify = require('vueify');
const root = path.resolve(__dirname, '..');
const source = path.join(root, 'src', 'javascript', 'main.js');
const build = path.join(root, 'dist');


/**
 * compile es6 to es5
 *
 * @method babel
 */
function babel(gulp, plugins) {
  return () => {
    const b = browserify(watchify.args);
    b.add(source);
    b.transform(babelify);
    b.transform(vueify);
    b.bundle()
      .pipe(vSource('dist.min.js'))
      .pipe(vBuffer())
      .pipe(plugins.sourcemaps.init())
      // .pipe(plugins.uglify())
      .pipe(plugins.sourcemaps.write('.'))
      .pipe(plugins.debug({ title: 'babel: built into' }))
      .pipe(gulp.dest(build));
    return b;
  };
}


module.exports = babel;
